<template>
  <div id="enterpriseCertification">
    <div class="title">企业信息编辑</div>
    <div class="message">
      <el-form
        :label-position="labelPosition"
        label-width="auto"
        :model="formLabelAlign"
        style="max-width: 600px"
        class="form"
      >
        <el-form-item label="企业名称">
          <el-input v-model="formLabelAlign.companyName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="企业地址">
          <el-input v-model="formLabelAlign.address" placeholder="请输入企业地址" />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="formLabelAlign.phoneNumber" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="企业类型">
          <el-select v-model="type" placeholder="请选择企业类型" size="large" style="width: 531px">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item style="margin-top: 15%; margin-left: 25%">
          <el-space :size="150">
            <el-button type="primary" @click="submitForm(ruleFormRef)">编辑</el-button>
            <el-button @click="resetForm(ruleFormRef)">重置</el-button>
          </el-space>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, type FormProps } from 'element-plus'
import { CompanyControllerService, CompanyUserControllerService } from '../../../generated'

const labelPosition = ref<FormProps['labelPosition']>('right')
const type = ref('')
const formLabelAlign = ref({
  companyName: '',
  address: '',
  type: '',
  phoneNumber: ''
})

const options = [
  { label: '发电企业', value: '发电企业' },
  { label: '电网企业', value: '电网企业' },
  { label: '电解铝企业', value: '电解铝企业' },
  { label: '民航企业', value: '民航企业' }
]

/**
 * 企业编辑
 * @param ruleFormRef
 */
const submitForm = async (ruleFormRef: any) => {
  const res = await CompanyControllerService.updateUsingPut(formLabelAlign.value)
  console.log(res)
  if (res.code != 0) {
    ElMessage({
      message: '企业编辑失败-数据库异常',
      type: 'error'
    })
  } else {
    ElMessage({
      message: '企业编辑成功',
      type: 'success'
    })
  }
}
/**
 * 重置
 * @param ruleFormRef
 */
const resetForm = (ruleFormRef: any) => {
  console.log('重置：', ruleFormRef)
}

const loadCompany = async () => {
  const res = await CompanyUserControllerService.getCompanyUsingGet()
  formLabelAlign.value.companyName = res.data.companyName
  formLabelAlign.value.address = res.data.address
  formLabelAlign.value.phoneNumber = res.data.phoneNumber
  type.value = res.data.type
}

onMounted(() => {
  loadCompany()
})
</script>

<style scoped>
.form {
  margin: 50px auto;
}

.layout-container-demo .el-aside {
  color: var(--el-text-color-primary);
  background: var(--el-color-primary-light-8);
  height: 100%;
}

.layout-container-demo .el-menu {
  border-right: none;
}

.layout-container-demo .el-main {
  padding: 0;
}

.container {
  width: 100%;
}

.title {
  height: 40px;

  line-height: 40px;
  background-color: rgb(247, 226, 196);
  padding-left: 20px;
  margin-left: -1%;
}

.message > div {
  text-align: center;
  margin-top: 30px;
}
</style>
